package io.renren.modules.gengyun.controller;


import io.renren.modules.gengyun.entity.PlanDate_Api;
import io.renren.modules.gengyun.entity.PlanGrowthPeriodEntity;
import io.renren.modules.gengyun.service.PlanGrowthPeriodService;
import io.renren.modules.gengyun.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/period")
@CrossOrigin
public class PlanGrowthPeriodComtroller {
    @Autowired
    private PlanGrowthPeriodService growth_periodService;
    @PostMapping("/find")
    public Map<String,Object> find(Long id) throws IOException {
        System.out.println(id);
        PlanGrowthPeriodEntity growth_period=growth_periodService.getPeriod(id);
        System.out.println(growth_period.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(growth_period,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/add")
    public Map<String,Object> add(PlanGrowthPeriodEntity growth_period) throws IOException {
        System.out.println(growth_period);
        int  flag =growth_periodService.save(growth_period);
        System.out.println(growth_period.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(PlanGrowthPeriodEntity growth_period) throws IOException {
        System.out.println(growth_period);
        int  flag =growth_periodService.update(growth_period);
        System.out.println(growth_period.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/delete")
    public Map<String,Object> delete(Long id) throws IOException {
        int  flag =growth_periodService.delete(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/all")
    public Map<String,Object> all() throws IOException {

        List<Map<String,Object>> allList= growth_periodService.allList();
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/bydate")
    public  Map<String,Object> bydate(PlanDate_Api date) throws IOException {
        System.out.println(date.getDate());
        List<Map<String,Object>> allList=  growth_periodService.findByDateList(date.getDate());
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }
}
